package org.iwidget.desktop.core.javascript;
import org.iwidget.desktop.core.Iwidget;
import org.iwidget.desktop.core.IwidgetMethods;
import org.iwidget.desktop.model.ElementPreferences;
import org.iwidget.desktop.model.WidgetElement;
import org.iwidget.model.javascript.FormElementJS;
import org.mozilla.javascript.*;
import org.mozilla.javascript.tools.shell.Environment;
import org.mozilla.javascript.tools.shell.Global;
/**
 *
 * @author Muhammad Hakim A
 */


public class IwidgetMethodsJS extends Global
{

    public IwidgetMethodsJS(Iwidget parent, Context cx)
    {
        super(cx);
        this.parent = parent;
        // functions names
        String names[] = {
            "currentTimeMillis", "alert", "question", "beep", "escape", "unescape", "input", "random", "closeWidget", "savePreferences", 
            "showWidgetPreferences", "sleep", "debug", "openURL", "form", "focusWidget", "run", "runInBg", "updateNow", "disableUpdates", 
            "enableUpdates"
        };
        
        try
        {
            defineFunctionProperties(names, org.iwidget.desktop.core.javascript.IwidgetMethodsJS.class, 2);
        }
        catch(/*PropertyException*/Exception e)
        {
            throw new Error();
        }
        
        Environment.defineClass(this);
        Environment environment = new Environment(this);
        defineProperty("environment", environment, 2);
    }

    public static Number currentTimeMillis()
    {
        return new Long(IwidgetMethods.currentTimeMillis());
    }

    public static void alert(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        IwidgetMethods.alert(widget, args[0].toString());
    }

    public static int question(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        return IwidgetMethods.question(widget, (String)args[0], (String)args[1]);
    }

    public static String input(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        return IwidgetMethods.input(widget, (String)args[0], (String)args[1]);
    }

    public static void beep(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        IwidgetMethods.beep();
    }

    public static String escape(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        String data = (String)Context.toType(args[0], java.lang.String.class);
        return IwidgetMethods.escape(data);
    }

    public static String unescape(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        String data = (String)Context.toType(args[0], java.lang.String.class);
        return IwidgetMethods.unescape(data);
    }

    public static int random(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        int start = 0;
        int end = 0x10000; // 65536
        if(args.length > 1)
        {
            start = Integer.parseInt(args[0].toString());
            end = Integer.parseInt(args[1].toString());
        } else
        if(args.length == 1)
            end = Integer.parseInt(args[0].toString());
        return IwidgetMethods.random(start, end);
    }

    public static void closeWidget(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        IwidgetMethods.closeWidget(widget);
    }

    public static void showWidgetPreferences(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        IwidgetMethods.showWidgetPreferences(widget);
    }

    public static void savePreferences(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        IwidgetMethods.savePreferences(widget);
    }

    public static void sleep(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        long time = Integer.parseInt(args[0].toString());
        IwidgetMethods.sleep(time);
    }

    public static void debug(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        IwidgetMethods.debug(widget, args[0].toString(), args[1].toString());
    }

    public static Object[] form(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        String title = args[1].toString();
        String saveLabel = null;
        String cancelLabel = null;
        if(args.length > 2)
            saveLabel = args[2].toString();
        if(args.length > 3)
            saveLabel = args[3].toString();
        NativeArray array = (NativeArray)args[0];
        ElementPreferences prefs[] = new ElementPreferences[(int)array.getLength()];
        for(int iCtr = 0; (long)iCtr < array.getLength(); iCtr++)
        {
            Object obj2 = array.get(iCtr, scope);
            FormElementJS item = (FormElementJS)Context.toType(obj2, org.iwidget.model.javascript.FormElementJS.class);
            prefs[iCtr] = item.getElementData();
        }

        return IwidgetMethods.form(widget, prefs, title, saveLabel, cancelLabel);
    }

    public static void openURL(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        String data = (String)Context.toType(args[0], java.lang.String.class);
        IwidgetMethods.openURL(data);
    }

    public static void focusWidget(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        IwidgetMethods.focusWidget(widget);
    }

    public static void updateNow(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        IwidgetMethods.updateNow(widget);
    }

    public static void disableUpdates(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        IwidgetMethods.disableUpdates(widget);
    }

    public static void enableUpdates(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        IwidgetMethods.enableUpdates(widget);
    }

    public static String run(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        String cmd = (String)Context.toType(args[0], java.lang.String.class);
        return IwidgetMethods.run(cmd);
    }

    public static void runInBg(Context cx, Scriptable thisObj, Object args[], Function funObj)
    {
        Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
        Object obj = scope.get("widget", scope);
        WidgetElement widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        String cmd = (String)Context.toType(args[0], java.lang.String.class);
        IwidgetMethods.runInBg(widget, cmd);
    }

    private static final long serialVersionUID = 0x2d37383334343532L;
    private static Iwidget parent;
}